home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / ENV Server / server src / aevent.c next >
Encoding:
C/C++ Source or Header  |  1993-05-31  |  6.2 KB  |  264 lines  |  [TEXT/ALFA]

  1. /*
  2. ** aevent.c
  3. **
  4. ** This module contains all the code for the program to handle the
  5. ** required Apple Events.  Of the four types, only two really
  6. ** pertain to the program:  open and quit.
  7. */
  8.  
  9. #include "es_headers"
  10.  
  11. #if LOGGING
  12. #    include <stdio.h>
  13. #    include "logging.h"
  14. #endif /* LOGGING */
  15.  
  16. #include <Errors.h>
  17. #include "envserver.h"
  18. #include "aevent.h"
  19. #include "hash.h"
  20. #include "utils.h"
  21. #include "LoadEnv.h"
  22.  
  23.  
  24. static pascal OSErr  QuitHandler    (AppleEvent*, AppleEvent*, long);
  25. static pascal OSErr  OAppHandler    (AppleEvent*, AppleEvent*, long);
  26. static pascal OSErr  ODocHandler    (AppleEvent*, AppleEvent*, long);
  27. static pascal OSErr  GetEnvHandler  (AppleEvent*, AppleEvent*, long);
  28. static pascal OSErr  PutEnvHandler  (AppleEvent*, AppleEvent*, long);
  29.  
  30. static void PostEnvReply( AppleEvent *, TargetID *, Ptr, long, OSType);
  31. static OSErr ESGetRequestParams(AppleEvent *, Ptr *, short *);
  32.  
  33.  
  34. void InitAEs(void)
  35. {
  36.     OSErr err;
  37.     
  38.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, QuitHandler, 0, FALSE);
  39.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, OAppHandler, 0, FALSE);
  40.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, ODocHandler, 0, FALSE);
  41.     err = AEInstallEventHandler( kESEventClass,   kESGetEnvMsg, GetEnvHandler, 0, FALSE);
  42.     err = AEInstallEventHandler( kESEventClass,   kESPutEnvMsg, PutEnvHandler, 0, FALSE);
  43.  
  44. } /* InitAEs */
  45.  
  46.  
  47.  
  48. void DoAppleEvent(EventRecord *theEvent)
  49. {
  50.     OSErr err;
  51.  
  52.     err = AEProcessAppleEvent(theEvent);  /* dispatch the handler for event. */
  53. } /* DoAppleEvent */
  54.  
  55.  
  56. /*===================================  HANDLERS FOLLOW... */
  57.  
  58. static pascal OSErr QuitHandler(AppleEvent *theAppleEvent,
  59.                 AppleEvent *reply, long handlerRefcon)
  60. {
  61.     extern Boolean gDone;
  62.     
  63.     gDone = TRUE;    /* don't call ExitToShell() here! or so says Apple... */
  64.     return noErr;
  65. } /* QuitHandler */
  66.  
  67.  
  68. static pascal OSErr OAppHandler(AppleEvent *theAppleEvent,
  69.                 AppleEvent *reply, long handlerRefcon)
  70. {
  71.     LoadEnvFile("Environment");
  72.     return noErr;
  73. } /* OAppHandler */
  74.  
  75.  
  76. static pascal OSErr ODocHandler(AppleEvent *theAppleEvent,
  77.                 AppleEvent *reply, long handlerRefcon)
  78. {
  79.     /* for each filename in the AEDescList, do: */
  80.     /*    get filename                          */
  81.     /*    LoadEnvFile( filename);               */
  82.     return noErr;
  83. } /* ODocHandler */
  84.  
  85.  
  86. /******************************************************/
  87. /*
  88. ** GetEnvHandler()
  89. */
  90. static pascal OSErr GetEnvHandler(AppleEvent *theAE,
  91.                 AppleEvent *reply, long handlerRefcon)
  92. {
  93.     HashNodePtr hnp;
  94.     OSErr err;
  95.     short msgLen;
  96.     Ptr msgBuf;
  97.     AEDescList paramList;
  98.     Boolean succOrFail = FALSE;
  99.  
  100.     /*
  101.     ** Get the parameters out:  the string length (typeShortInteger)
  102.     ** and the string's bytes.  (typeChar).
  103.     **
  104.     ** Put the string (converted to a C string) into msgBuf.
  105.     ** Put the length of the string into msgLen.
  106.     */
  107.  
  108.     err = ESGetRequestParams( theAE, &msgBuf, &msgLen);
  109.  
  110.     if (err != noErr)
  111.     {
  112.         if (msgBuf != NULL)
  113.             DisposPtr(msgBuf);
  114.         return err;
  115.     }
  116.     
  117.     /*
  118.     ** At this point, we've received the request; msgBuf contains the C string 
  119.     ** which is the name of the environment variable.  Now look up that var
  120.     ** name, and if it exists, return the var's string value.
  121.     */
  122.     
  123.     hnp = hash_get_entry( msgBuf);
  124.  
  125.     if (hnp == NULL)
  126.         return errESNotFound;
  127.     
  128.     /*
  129.     ** OK, now add params to the reply event and return error code.
  130.     */
  131.     
  132.     DisposPtr( msgBuf);
  133.     msgLen = mystrlen((char*)hnp->value);
  134.     msgBuf = NewPtr( msgLen+1 );
  135.     if (msgBuf == NULL)
  136.         return memFullErr;
  137.     succOrFail = TRUE;
  138.     mystrcpy( msgBuf, hnp->value);
  139.     
  140.     
  141.     err = AEPutKeyPtr( reply, keyESFlag, typeBoolean, (Ptr)&succOrFail,
  142.                 sizeof(succOrFail));
  143.     if (err != noErr)
  144.         return err;
  145.  
  146.     err = AEPutKeyPtr( reply, keyESLen, typeShortInteger, (Ptr)&msgLen,
  147.                 sizeof(msgLen));
  148.     if (err != noErr)
  149.         return err;
  150.  
  151.     /*
  152.     ** Note that I just return the string's raw bytes, no C trailing nul
  153.     ** nor Pascal's preceding length byte.
  154.     */
  155.     err = AEPutKeyPtr( reply, keyESString, typeChar, msgBuf, msgLen);
  156.     
  157.     return err;
  158.     
  159. } /* GetEnvHandler */
  160.  
  161.  
  162. /******************************************************/
  163. /*
  164. ** PutEnvHandler()
  165. */
  166. static pascal OSErr PutEnvHandler(AppleEvent *theAE,
  167.                 AppleEvent *reply, long handlerRefcon)
  168. {
  169.     OSErr err;
  170.     short msgLen;
  171.     Ptr msgBuf;
  172.     AEDescList paramList;
  173.     Boolean succOrFail = FALSE;
  174.     int herr;
  175.  
  176.     /*
  177.     ** Get the parameters out:  the string length (typeShortInteger)
  178.     ** and the string's bytes.  (typeChar).
  179.     **
  180.     ** Put the string (converted to a C string) into msgBuf.
  181.     ** Put the length of the string into msgLen.
  182.     */
  183.  
  184.     err = ESGetRequestParams( theAE, &msgBuf, &msgLen);
  185.  
  186.     if (err != noErr)
  187.     {
  188.         if (msgBuf != NULL)
  189.             DisposPtr(msgBuf);
  190.         return err;
  191.     }
  192.     
  193.     /*
  194.     ** At this point, we've received the request; msgBuf contains the C string 
  195.     ** which is the expression  "var=value" where var is the name of the
  196.     ** environment variable and value is the string value of it.
  197.     */
  198.     
  199.     herr = hash_add_entry( msgBuf);
  200.     DisposPtr( msgBuf);
  201.  
  202.     if ( herr != H_NOERR )
  203.         return errESNotFound;
  204.     
  205.     /*
  206.     ** OK, now add params to the reply event and return error code.
  207.     */
  208.     succOrFail = TRUE;
  209.     
  210.     err = AEPutKeyPtr( reply, keyESFlag, typeBoolean, (Ptr)&succOrFail,
  211.                 sizeof(succOrFail));
  212.     return err;
  213.  
  214. } /* PutEnvHandler */
  215.  
  216.  
  217.  
  218. /*******************
  219. ** ESGetRequestParams
  220. **
  221. ** This is called by the getenv() and putenv() handlers;
  222. ** It extracts the request (a length short and a string)
  223. ** from the apple event pointed to by theAE.  Once
  224. ** finished processing, *msgBuf will contain a C string
  225. ** and *msgLen will be the string's length.
  226. *******************/
  227. OSErr ESGetRequestParams(AppleEvent *theAE, Ptr *msgBuf, short *msgLen)
  228. {
  229.     OSErr err;
  230.     AEDescList paramList;
  231.     DescType foo;
  232.     Size actualSize;
  233.     Ptr tempBuf;
  234.     short tempLen;
  235.     
  236.     /** Fetch the length of the string from the Apple Event **/
  237.     
  238.     err = AEGetKeyPtr( theAE, keyESLen, typeShortInteger, &foo,
  239.                         (Ptr)&tempLen, sizeof(short), &actualSize);
  240.     if (err != noErr)
  241.         return err;
  242.     
  243.     
  244.     tempBuf = NewPtr( tempLen);  /* get buffer space plus NULL char */
  245.     if (tempBuf == NULL)
  246.         return memFullErr;
  247.     
  248.     
  249.     /** Now fetch the actual string, converting it into a C string **/
  250.     
  251.     err = AEGetKeyPtr( theAE, keyESString, typeChar, &foo,
  252.                 tempBuf, tempLen, &actualSize);
  253.     if (err != noErr)
  254.         return err;
  255.     tempBuf[ tempLen] = 0;  /* terminate string */
  256.     
  257.     *msgBuf = tempBuf;
  258.     *msgLen = tempLen;
  259.     
  260.     return err;
  261. } /* ESGetRequestParams */
  262.  
  263.  
  264.